package com.example.demo;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class UserService {


    public List<User> users = new ArrayList<User>();

    public User getUserById(long id){

        return users.stream().filter(u->u.getId()==id).findFirst().orElse(null);

    }

    public List<User> getAllUser(){

        return new ArrayList<>(users);

    }


    public boolean createUser(User user){

        if(getUserById(user.getId())!=null)
            return false;


        users.add(user);
        return true;

    }

    public boolean deleteUser(long id){

        User findUser=getUserById(id);
        if(findUser==null)
            return false;

        users.remove(findUser);
        return true;

    }


    public boolean updateUser(User user){

        User findUser=getUserById(user.getId());

        if(findUser==null)
            return false;

        users.set(users.indexOf(findUser),user);
        return true;


    }



}
